package com.happyrecyclingstation.mapper;

import com.happyrecyclingstation.entity.Message;
import com.happyrecyclingstation.vo.ContactVO;
import com.happyrecyclingstation.vo.MessageVO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MessageMapper {
    /**
     * 通过id获取联系人列表
     * @param id
     * @return
     */
    List<ContactVO> getContact(Integer id);

    /**
     * 通过sessionId获取聊天记录
     * @param sessionId
     * @return
     */
    @Select("select sender_id, content, send_time from lzxm_message where session_id = #{sessionId}")
    List<MessageVO> getHistoryMsg(String sessionId);

    /**
     * 保存消息
     * @param message
     */
    @Select("insert into lzxm_message(sender_id, receiver_id, content, session_id, send_time) values(#{senderId}, #{receiverId}, #{content}, #{sessionId}, #{sendTime})")
    void saveMessage(Message message);

    /**
     * 判断是否建立联系关系
     * @param currentId
     * @param hr
     * @return
     */
    @Select("select count(*) from lzxm_contact where (user_id = #{currentId} and friend_id = #{hr}) or (user_id = #{hr} and friend_id = #{currentId})")
    Integer selectIsContact(Integer currentId, Integer hr);

    /**
     * 保存联系人关系
     * @param currentId
     * @param hr
     */
    @Insert("insert into lzxm_contact(user_id, friend_id) values(#{currentId}, #{hr}), (#{hr}, #{currentId})")
    void saveContact(Integer currentId, Integer hr);
}
